//
//  YGHttpManager.m
//  research
//
//  Created by 章鱼 on 17/3/28.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "YGHttpManager.h"
#import "GYCommonModel.h"

@interface YGHttpManager ()


@end

@implementation YGHttpManager

+ (id)defaultManager {
    static YGHttpManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[YGHttpManager alloc] init];
    });
    return manager;
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        _httpManager = [VBHTTPManager defaultManager];
        
    }
    return self;
}


//1.首页banner接口
- (void)fetchGetBannersuccess:(requestSuccessBlock)successHandler
                      failure:(requestFailureBlock)failureHandler{
    [self fetchFromUrl:GY_BANNER_URL
                params:@{}
               success:successHandler
               failure:failureHandler];
}

//2.新闻列表
- (void)fetchGetNewsListWithPage:(NSInteger )page Pagesize:(NSInteger )pagesize
                         success:(requestSuccessBlock)successHandler
                         failure:(requestFailureBlock)failureHandler{

    [self fetchFromUrl:GY_NEWSLIST_URL
                params:@{@"page": [NSString stringWithFormat:@"%ld",(long)page],
                         @"pageSize": [NSString stringWithFormat:@"%ld",(long)pagesize]
                         }
               success:successHandler
               failure:failureHandler];
}
//3.新闻详情
- (void)fetchGetNewsInfoWithNewsId:(NSInteger )newsId
                           success:(requestSuccessBlock)successHandler
                           failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_NEWSINFO_URL
                params:@{@"newsId": [NSString stringWithFormat:@"%ld",(long)newsId]
                         }
               success:successHandler
               failure:failureHandler];
}
//4.问卷列表
- (void)fetchGetQueslistWithPage:(NSInteger )page
                        Pagesize:(NSInteger )pagesize
                         success:(requestSuccessBlock)successHandler
                         failure:(requestFailureBlock)failureHandler {
    
    [self fetchFromUrl:GY_QUESTIONLIST_URL
                params:@{@"page": [NSString stringWithFormat:@"%ld",(long)page],
                         @"pageSize": [NSString stringWithFormat:@"%ld",(long)pagesize]
                         }
               success:successHandler
               failure:failureHandler];

}
//5.问卷详情
- (void)fetchGetQuestInfoWithQuestionId:(NSInteger )questionId
                                success:(requestSuccessBlock)successHandler
                                failure:(requestFailureBlock)failureHandler{

    [self fetchFromUrl:GY_QUESTIONINFO_URL
                params:@{@"questionId": [NSString stringWithFormat:@"%ld",(long)questionId]
                         
                         }
               success:successHandler
               failure:failureHandler];
}

//6.问卷答题
- (void)fetchGetQuestCollectWithToken:(NSString *)token
                               userId:(NSString *)userId
                           questionId:(NSString *)questionId
                              success:(requestSuccessBlock)successHandler
                              failure:(requestFailureBlock)failureHandler {
    [self fetchFromUrl:GY_QUESTIONCOLLECT_URL
                params:@{@"token": token,
                         @"userId": userId,
                         @"questionId":questionId
                         }
               success:successHandler
               failure:failureHandler];

}

//7.问卷收藏列表查询
- (void)fetchGetUserCollectdWithToken:(NSString *)token
                               userId:(NSString *)userId
                                 Page:(NSInteger )page
                             Pagesize:(NSInteger )pagesize
                              success:(requestSuccessBlock)successHandler
                              failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_COLLECTED_URL
                params:@{@"token": token,
                         @"userId": userId,
                         @"page": [NSString stringWithFormat:@"%ld",(long)page],
                         @"pageSize": [NSString stringWithFormat:@"%ld",(long)pagesize]
                         }
               success:successHandler
               failure:failureHandler];

}

//8.个人信息查询
- (void)fetchGetUserInfoWithToken:(NSString *)token
                           userId:(NSString *)userId
                          success:(requestSuccessBlock)successHandler
                          failure:(requestFailureBlock)failureHandler{
    
    [self fetchFromUrl:GY_USERINFO_URL
                params:@{@"token": token,
                         @"userId": userId
                         }
               success:successHandler
               failure:failureHandler];

}
//9.修改密码
- (void)fetchGetUserModifyPasswdWithToken:(NSString *)token
                                   userId:(NSString *)userId
                                newPasswd:(NSString *)newPasswd
                                oldPasswd:(NSString *)oldPasswd
                                  success:(requestSuccessBlock)successHandler
                                  failure:(requestFailureBlock)failureHandler{
    [self fetchFromUrl:GY_USERMODIFYPASSWD_URL
                params:@{@"token": token,
                         @"userId": userId,
                         @"newPasswd": newPasswd,
                         @"oldPasswd": oldPasswd
                         }
               success:successHandler
               failure:failureHandler];

}

//10.登录
- (void)fetchGetUserLoginWithuserName:(NSString *)userName
                             password:(NSString *)password
                              success:(requestSuccessBlock)successHandler
                              failure:(requestFailureBlock)failureHandler {


    [self fetchFromUrl:GY_USERLOGIN_URL
                params:@{@"userName": userName,
                         @"password": password,
                         }
               success:successHandler
               failure:failureHandler];
}

//11.注册
- (void)fetchGetUserRegisterWithuserName:(NSString *)userName
                                password:(NSString *)password
                                   email:(NSString *)email
                                   phone:(NSString *)phone
                                 success:(requestSuccessBlock)successHandler
                                 failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_USERREGISTER_URL
                params:@{@"userName": userName,
                         @"password": password,
                         @"email": email,
                         @"phone": phone,
                         }
               success:successHandler
               failure:failureHandler];
    

}
//12.判断是否登录
- (void)fetchGetUserIsloginWithtoken:(NSString *)token
                             success:(requestSuccessBlock)successHandler
                             failure:(requestFailureBlock)failureHandler {
    [self fetchFromUrl:GY_USERISLOGIN_URL
                params:@{@"token": token,
                         }
               success:successHandler
               failure:failureHandler];

}
//13.修改个人信息
- (void)fetchGetUserSettingWithtoken:(NSString *)token
                              userId:(NSString *)userId
                            nickname:(NSString *)nickname
                               phone:(NSString *)phone
                               email:(NSString *)email
                             address:(NSString *)address
                              gender:(NSString *)gender
                             success:(requestSuccessBlock)successHandler
                             failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_USERSETTING_URL
                params:@{@"token": token,
                         @"userId": userId,
                         @"nickname": nickname,
                         @"phone": phone,
                         @"email": email,
                         @"address": address,
                         @"gender": gender,
                         }
               success:successHandler
               failure:failureHandler];

}

//14.微信登录
- (void)fetchGetUserWXLoginWithcode:(NSString *)code
                            success:(requestSuccessBlock)successHandler
                            failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_WXLOGIN_URL
                params:@{@"code": code,
                         }
               success:successHandler
               failure:failureHandler];

    
}

//15.忘记密码
- (void)fetchGetPassWordWithWithphone:(NSString *)phone
                                email:(NSString *)email
                             newPasswd:(NSString *)newPasswd
                              success:(requestSuccessBlock)successHandler
                              failure:(requestFailureBlock)failureHandler {

    [self fetchFromUrl:GY_FORGETPASSWORD_URL
                params:@{@"phone": phone,
                         @"email": email,
                         @"newPasswd": newPasswd,
                         }
               success:successHandler
               failure:failureHandler];

}


#pragma mark - base
- (void)fetchFromUrl:(NSString *)url
              params:(NSDictionary *)params
             success:(requestSuccessBlock)successHandler
             failure:(requestFailureBlock)failureHandler {
    NSString *baseUrl = [APOSERVER_BASE_URL stringByAppendingString:url];
    [[VBHTTPManager defaultManager] postRequest:baseUrl
                                         params:params
                                        success:^(id responseObj) {
                                        successHandler(responseObj);
  
                                        } failure:^(NSError *error) {
                                            failureHandler(error);
                                        }];
}
#pragma mark 上传图片
/**
 文件上传
 */
- (void)updateRequest:(NSString *)url
               params:(NSDictionary *)params
           fileConfig:(VBFileConfig *)fileConfig
              success:(requestSuccessBlock)successHandler
              failure:(requestFailureBlock)failureHandler {

    NSString *baseUrl = [APOSERVER_BASE_URL stringByAppendingString:url];
    
    [[VBHTTPManager defaultManager] updateRequest:baseUrl params:params fileConfig:fileConfig success:^(id responseObj) {
        
        successHandler(responseObj);
        
    } failure:^(NSError *error) {

        
        failureHandler(error);
    }];
}



@end
